const rsa_public_key = '-----BEGIN PUBLIC KEY-----\n' +
    'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDUMkU7SuVf3RYuNYyG3Lexg5mP\n' +
    '0cfv3IJ5jIgsLR4jIpl3w1nnqIPfjHOaztel34X45QfCIIBjwX0gUadxax4//pWo\n' +
    't+VzDKfT746zZ8OcB5dFoAxkd78FDE9hg2r2BD6mhHrLMFQrXdI2aUMy9FNKDHtE\n' +
    '07lO2rjjEOy5yQf7cwIDAQAB\n' +
    '-----END PUBLIC KEY-----'

const rsa_private_key = '-----BEGIN RSA PRIVATE KEY-----\n' +
    'MIICWwIBAAKBgGhUWL9F/8/+LHSk4M5Vh8YLTKI6/44WsX8ZLJQONoGBF9zL1rc8\n' +
    '7IkliSgGPYb+vyC6gxYZw+RmetJuTUShnRhY1Ba++MZwGLgY0cjlh26IvkOLJuLk\n' +
    '+gIeERnErhQ41fWurmnhiCpYYGsPaXgsVIOgSan9+uih+4BfRaItm6LFAgMBAAEC\n' +
    'gYALy82lAtMjv0djgOjyK3WIjOqDRUxz0lkLV9tUei7rgZ8jFeEXIspTz2ysm9gF\n' +
    'FXSizLPZodwmTPgCJvqRkfxjCWFrg3wqY8mW1j+RK7UeRc2o2ZZcfXbFPwdtqntA\n' +
    'GE6YYsyOQjDn6bIZ8EL8ViQdTKltVmS7+VAiviTGiGNmQQJBAKarjjasccbwTbSD\n' +
    'xILfIwSb7tAv3iAgjl4vWn6V3urePQV2yX0q4TcNV2izuFpR9tmyMwDaYpe8sLbC\n' +
    'J2bdONkCQQCgPyp2NMX6wsELKD3l7fG6lX267E5crbChP8M3I2G/mz9H1Ck+vG1Q\n' +
    'yvh76GA4zxfFC+QqlvDt+zvIlze2k+XNAkEApiLWBLvKQkKhQnIifU3NizegSpec\n' +
    'dCvfnhTXwNzFsxrKetrYp9MJYiYDXlPfTPDs/j16R7xokNY+1D4eppOXqQJAW6fl\n' +
    'dJj4yFYOroAE73eqtKr+sCSrKY1bIs/iq638Iej9R+tM8Ao8iGKfv9eI+dVR5i3I\n' +
    'Aqg1BBmX90Ku5iov3QJAGeBsgSpZIO3c+nKWTl1E128iWh8q/0M2NQX+jxvz4tNj\n' +
    'NxiTI7EtPu3QULRB1F4SBJMtcvxPR4gR85ArXQGfDg==\n' +
    '-----END RSA PRIVATE KEY-----'

// 加密
function encrypt(data) {
    let encrypt = new JSEncrypt();
    encrypt.setPublicKey(rsa_public_key);
    return encrypt.encryptLong(data);
}

// 解密
function decrypt(data) {
    let decrypt = new JSEncrypt();
    decrypt.setPrivateKey(rsa_private_key);
    return decrypt.decryptLong(data);
}

// 登录
$("#userLogin").on("click", function () {

    let username = $("input[name='username']").val(),
        password = $("input[name='password']").val();

    let data = {username: username, password: password};

    data = {token: encrypt(JSON.stringify(data))};

    $.ajax({
        type: 'POST',
        url: "login",
        data: data,
        success: function (res) {
            console.log(res);
            console.log(decrypt(res));
            if (decrypt(res)) {
                let result = JSON.parse(decrypt(res));
                if (result.code == 200) {
                    localStorage.setItem('Authorization', result.data)
                    window.location.href = 'index';
                } else {
                    alert(result.msg);
                    return;
                }
            }

        },
        error: function (res) {
            console.log(res)
        }
    });
});

$('#getUserInfo').on('click', function () {

    $.ajax({
        type: 'POST',
        url: "index",
        data: {},
        beforeSend: function (request) {
            request.setRequestHeader("Authorization", 'Bearer ' + localStorage.getItem("Authorization"));
        },
        success: function (res) {
            console.log(res);
            console.log(decrypt(res));
            if (decrypt(res)) {
                let result = JSON.parse(decrypt(res));
                if (result.code == 200) {
                    alert(result.msg);
                    return;
                } else {
                    alert(result.msg);
                    return;
                }
            }

        },
        error: function (res) {
            console.log(res)
        }
    });


});